﻿using UnityEngine;
using System.Collections;

public class SideTrackAI : MonoBehaviour {

	public float Speed = 0.05f;

	private GameObject player = null;

	// Use this for initialization
	void Start () {
		findPlayer();
	}

	private void findPlayer () {
		player = GameObject.FindGameObjectWithTag("Player");
	}
	
	// Update is called once per frame
	void Update () {
		if (player) {
			float deltaX = player.transform.position.x - transform.position.x;
			float maxDistance = Time.deltaTime * Speed;
			if (deltaX * deltaX < maxDistance * maxDistance) {
				transform.Translate(Vector3.right * deltaX);
			}
			else {
				if (deltaX > 0.0f) {
					transform.Translate(Vector3.right * Speed * Time.deltaTime);
				}
				else {
					transform.Translate(Vector3.left * Speed * Time.deltaTime);
				}
			}
		}
	}
}
